package com.zqt.zqt805.controller;

import com.zqt.zqt805.domain.User;
import com.zqt.zqt805.service.UserService;
import com.zqt.zqt805.util.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "用户管理接口---张淇童0805")
@RestController
@RequestMapping("/api/users")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "用户注册", notes = "用户注册接口")
    @PostMapping("/register")
    public AjaxResult register(@RequestBody User user) {
        userService.register(user);
        return AjaxResult.success("User 注册 successfully");
    }

    @ApiOperation(value = "用户登录", notes = "用户登录接口")
    @PostMapping("/login")
    public AjaxResult login(@RequestParam String username, @RequestParam String password) {
        String loginmsg = userService.login(username, password);
        return AjaxResult.success("登录 successful",loginmsg);
    }

    @ApiOperation(value = "获取用户信息", notes = "根据用户ID获取用户信息")
    @GetMapping("/{user_id}")
    public AjaxResult getUserInfo(@ApiParam(value = "用户ID", required = true) @PathVariable int user_id) {
        User user = userService.getUserById(user_id);
        return AjaxResult.success(user);
    }
}